package sit.mycatalog.util;

import javax.naming.Context;
import javax.naming.InitialContext;

import sit.mycatalog.ejb.sess.CatnodeSess;
import sit.mycatalog.ejb.sess.ItemSess;
import sit.mycatalog.ejb.sess.SysuserSess;

public class SessLookup {
	private static final String m_strPrefix = "java:app/mycatalog-ejb/";
	private Context ctx;
	private static SessLookup instance;
	
	private SessLookup() throws Exception {
		ctx = new InitialContext();
	}
	
	private static SessLookup getInstance() throws Exception {
		if(instance == null) {
			instance = new SessLookup();
		}
		
		return instance;
	}
	
	private Object lookupJNDI(String p_strJNDIName) throws Exception {
		Object objRef = null;
		
		try {
			objRef = ctx.lookup(p_strJNDIName);
		} catch (Exception e) {
			throw new Exception("lookupJNDI(): " + e.getMessage(), e); 
		}
		
		return objRef;
	}
	
	public static CatnodeSess getCatnodeSess() throws Exception {
		CatnodeSess sessCatnode;
		
		try {
			sessCatnode = (CatnodeSess)getInstance().lookupJNDI(m_strPrefix + "CatnodeSess");
		} catch (Exception e) {
			throw new Exception(e.getMessage(), e);
		}
		
		return sessCatnode;
	}
	
	public static ItemSess getItemSess() throws Exception {
		ItemSess sessItem;
		
		try {
			sessItem = (ItemSess)getInstance().lookupJNDI(m_strPrefix + "ItemSess");
		} catch (Exception e) {
			throw new Exception(e.getMessage(), e);
		}
		
		return sessItem;
	}
	
	public static SysuserSess getSysuserSess() throws Exception {
		SysuserSess sessSysuser;
		
		try {
			sessSysuser = (SysuserSess)getInstance().lookupJNDI(m_strPrefix + "SysuserSess");
		} catch (Exception e) {
			throw new Exception(e.getMessage(), e);
		}
		
		return sessSysuser;
	}
}
